package com.fsnbigdata.repository;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.ProductOrganism;
import com.fsnbigdata.domain.Product;
import com.fsnbigdata.domain.QProductOrganism;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = ProductOrganismRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = ProductOrganismRepositoryCustom.class)
@Transactional(readOnly = true)
public class ProductOrganismRepositoryImpl extends QueryDslRepositorySupportExt<ProductOrganism> implements ProductOrganismRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    ProductOrganismRepositoryImpl() {
        super(ProductOrganism.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PRODUCT = "product";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM_ID = "organismId";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String LATIN = "latin";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    /**
     * TODO Auto-generated method documentation
     *
     * @param product
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<ProductOrganism> findByProduct(Product product, GlobalSearch globalSearch, Pageable pageable) {
        QProductOrganism productOrganism = QProductOrganism.productOrganism;
        JPQLQuery<ProductOrganism> query = from(productOrganism);
        Assert.notNull(product, "product is required");
        query.where(productOrganism.product.eq(product));
        Path<?>[] paths = new Path<?>[] { productOrganism.product, productOrganism.organismId, productOrganism.name, productOrganism.latin };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(PRODUCT, productOrganism.product).map(ORGANISM_ID, productOrganism.organismId).map(NAME, productOrganism.name).map(LATIN, productOrganism.latin);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, productOrganism);
    }
}
